package com.jiangjiang.eshop.commodity.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.commodity.convert.GoodsSkuConvert;
import com.jiangjiang.eshop.commodity.convert.GoodsSkuSalePropertyValueConvert;
import com.jiangjiang.eshop.commodity.dao.GoodsSkuMapper;
import com.jiangjiang.eshop.commodity.domain.CategoryPropertyRelationshipEntity;
import com.jiangjiang.eshop.commodity.domain.GoodsSkuEntity;
import com.jiangjiang.eshop.commodity.domain.GoodsSkuSalePropertyValueEntity;
import com.jiangjiang.eshop.commodity.dto.GoodsSkuDTO;
import com.jiangjiang.eshop.commodity.dto.GoodsSkuPageQueryDTO;
import com.jiangjiang.eshop.commodity.dto.GoodsSkuSalePropertyValueDTO;
import com.jiangjiang.eshop.commodity.dto.PropertyDTO;
import com.jiangjiang.eshop.inventory.service.InventoryService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.krb5.internal.TGSRep;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName GoodsSkuMapper
 * @Description TODO
 * @Author jhw
 * @Date 2021/8/2 18:02
 * @Version 1.0
 **/
@Service
public class GoodsSkuService extends ServiceImpl<GoodsSkuMapper, GoodsSkuEntity> {

    @Autowired
    private GoodsSkuConvert goodsSkuConvert;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private GoodsSkuSalePropertyValueService goodsSkuSalePropertyValueService;
    @Autowired
    private CategoryPropertyRelationshipService categoryPropertyRelationshipService;
    @Autowired
    private PropertyService propertyService;
    @Autowired
    private GoodsSkuSalePropertyValueConvert goodsSkuSalePropertyValueConvert;


    /**
     * 根据商品名称查询sku数据
     *
     * @param goodsId 商品id
     * @return
     */
    public List<GoodsSkuDTO> queryByGoodsId(Long goodsId) {
        LambdaQueryWrapper<GoodsSkuEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsSkuEntity::getGoodsId, goodsId);
        return goodsSkuConvert.convertEntities2DTOList(this.list(wrapper))
                .stream()
                .peek(skuDTO -> {
                    Long saleStockQuantity = inventoryService.getSaleStockQuantity(skuDTO.getId());
                    List<GoodsSkuSalePropertyValueDTO> dtoList = goodsSkuSalePropertyValueService.queryByGoodsSkuId(skuDTO.getId());
                    skuDTO.setSaleStockQuantity(saleStockQuantity);
                    skuDTO.setPropertyValues(dtoList);
                }).collect(Collectors.toList());
    }


    /**
     * 批量新增商品数据
     *
     * @param goodsSkuList 商品sku
     * @return 处理结果
     */
    public Boolean batchSave(List<GoodsSkuDTO> goodsSkuList) {

        for (GoodsSkuDTO goodsSkuDTO : goodsSkuList) {

            String saleProperties = getSaleProperties(goodsSkuDTO.getPropertyValues());
            goodsSkuDTO.setSaleProperties(saleProperties);
            GoodsSkuEntity goodsSkuEntity = goodsSkuConvert.convertDTO2Entity(goodsSkuDTO);
            this.save(goodsSkuEntity);

            List<GoodsSkuSalePropertyValueEntity> propertyValueEntities = goodsSkuDTO.getPropertyValues().stream()
                    .map(propertyValue -> {
                        propertyValue.setGoodsSkuId(goodsSkuEntity.getId());
                        return goodsSkuSalePropertyValueConvert.convertDTO2Entity(propertyValue);
                    }).collect(Collectors.toList());
            goodsSkuSalePropertyValueService.saveBatch(propertyValueEntities);

            inventoryService.setSaleStockQuantity(goodsSkuEntity.getId(), goodsSkuDTO.getSaleStockQuantity());
        }

        return true;
    }

    /**
     * 获取销售属性
     *
     * @param propertyValues 销售属性值
     * @return 处理结果
     */
    private String getSaleProperties(List<GoodsSkuSalePropertyValueDTO> propertyValues) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < propertyValues.size(); i++) {
            CategoryPropertyRelationshipEntity relation = categoryPropertyRelationshipService.getById(propertyValues.get(i).getRelationId());
            PropertyDTO propertyDTO = propertyService.queryById(relation.getPropertyId());
            builder.append(propertyDTO.getPropertyName()).append(":").append(propertyValues.get(i).getPropertyValue());
            if (i < propertyValues.size() - 1) {
                builder.append(";");
            }
        }
        return builder.toString();
    }

    /**
     * 根据商品id删除sku数据
     *
     * @param goodsId 商品id
     * @return 处理结果
     */
    public Boolean removeByGoodsId(Long goodsId) {
        List<Long> skuIds = this.queryByGoodsId(goodsId).stream().map(GoodsSkuDTO::getId).collect(Collectors.toList());
        goodsSkuSalePropertyValueService.batchRemoveBySkuIds(skuIds);

        LambdaQueryWrapper<GoodsSkuEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsSkuEntity::getGoodsId, goodsId);
        return this.remove(wrapper);
    }

    /**
     * 略
     * @param dto
     * @return
     */
    public Page<GoodsSkuDTO> queryForPage(GoodsSkuPageQueryDTO dto) {
        return null;
    }
}
